﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Public;

namespace QLNhanKhau
{
    public partial class UCKhaiSinh : UserControl
    {
        public UCKhaiSinh()
        {
            InitializeComponent();
        }
        private void UCKhaiSinh_Load(object sender, EventArgs e)
        {
            txtSoSHK.Text = "1000002";
            //ghi chú xóa.
            txtSoSHK.Focus();
            txtMaKS.Text = DungChung.LoadMaNK().ToString();
            txtMaKS.Enabled = false;

            //load dân tộc
            cmbDanToc.DataSource = DanTocBLL.LoadDanToc();
            cmbDanToc.DisplayMember = "TenDanToc";
            cmbDanToc.ValueMember = "MaDT";
            //load quoc tich
            cmbQuocTich.DataSource = QuocTichBLL.LoadQuocTich();
            cmbQuocTich.DisplayMember = "TenQuocTich";
            cmbQuocTich.ValueMember = "MaQT";
            //load quan hệ chủ hộ
            cmbQuanHeChuHo.DataSource = QuanHeBLL.LoadQuanHe();
            cmbQuanHeChuHo.DisplayMember = "TenQuanHe";
            cmbQuanHeChuHo.ValueMember = "MaQH";
            //load quan hệ khai sinh
            cmbQuanHeKS.DataSource = QuanHeBLL.LoadQuanHe();
            cmbQuanHeKS.DisplayMember = "TenQuanHe";
            cmbQuanHeKS.ValueMember = "MaQH";
           
            groupBox1.Enabled = false;
            groupBox2.Enabled = false;

            cmbNguoiNhan.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiNhan.DisplayMember = "HoTen";
            cmbNguoiNhan.ValueMember = "MaND";

            //cmbCanBoDangKi.Text = frmDangNhap.user_login.HoTen.ToString();
            cmbCanBoDangKi.Items.Add(frmDangNhap.user_login.HoTen.ToString());
            cmbCanBoDangKi.SelectedIndex = 0;
            //.SelectedIndex = 1;
            //cmbCanBoDangKi.SelectedValue = frmDangNhap.user_login.MaND;
            //cmbCanBoDangKi.SelectedItem = frmDangNhap.user_login.MaND;
            //cmbCanBoDangKi.v = frmDangNhap.user_login.MaND;
            //load chuc vu

            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVuNguoiNhan.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuNguoiNhan.Enabled = false;

            dateNgayNhap.Value = DateTime.Parse(DateTime.Now.ToString());

            //load cho phần khai sinh

            cmbNguoiCap.DataSource = NguoiDungBLL.LoadChuTich();
            cmbNguoiCap.DisplayMember = "HoTen";
            cmbNguoiCap.ValueMember = "MaND";

            cmbCanBoHoTich.DataSource = NguoiDungBLL.LoadCanBoHoTich();
            cmbCanBoHoTich.DisplayMember = "HoTen";
            cmbCanBoHoTich.ValueMember = "MaND";

            txtChucVu.Text = ChucVuBLL.LoadChucVuByMaCV(NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).MaCV).TenChucVu;
            txtNoiCap.Text = ThietLapBLL.LoadDiaChi();
            //to màu
            cmbNoiSinh.DataSource = TinhBLL.LoadTinh();
            cmbNoiSinh.DisplayMember = "TenTinh";
            cmbNoiSinh.ValueMember = "MaT";
            
        }
        private int _MaSHK;

        public int MaSHK
        {
            get { return _MaSHK; }
            set { _MaSHK = value; }
        }
        private void btnTim_Click(object sender, EventArgs e)
        {
            if (txtSoSHK.Text == "")
            {
                MessageBox.Show("Nhập số sổ hộ khẩu cần tìm", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtSoSHK.Focus();
                return;
                
            }
            int SoSHK = int.Parse(txtSoSHK.Text);
            SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
            if (shk == null)
            {
                txtDuong.Text = txtAp.Text = txtSoNha.Text = txtChuHo.Text = "";
                dgvNhanKhau.DataSource = null;
                groupBox1.Enabled = false;
                groupBox2.Enabled = false;
                MessageBox.Show("Không tìm thấy!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSoSHK.ResetText();
                txtSoSHK.Focus();
                return;
            }
            else
            {
                
                _MaSHK = shk.MaSHK;
                groupBox1.Enabled = true;
                groupBox2.Enabled = true;
                //SoHoKhauPublic shk = SoHoKhauBLL.LoadSoHoKhauBySoSHK(SoSHK);
                List<NhanKhauPublic> list_nk = NhanKhauBLL.LoadNhanKhauByMaSHK(shk.MaSHK);
                List<NhanKhauPublic> list_nkc = NhanKhauBLL.LoadNhanKhauByMaSHKCha(shk.MaSHK);
                List<NhanKhauPublic> list_nkm = NhanKhauBLL.LoadNhanKhauByMaSHKMe(shk.MaSHK);
                txtChuHo.Text = list_nk.Find(item => item.MaQH == 1).HoTen;

                //load hotencha me
                
                dgvNhanKhau.DataSource = list_nk;
                //dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
                dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
                //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
                dgvNhanKhau.Columns[4].HeaderText = "CMND";
                dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
                //dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
                dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
                dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
                dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
                dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
                dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
                //dgvNhanKhau.Columns[12].HeaderText = "Chứng tử";
                dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
                dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
                //dgvNhanKhau.Columns[15].HeaderText = "Sổ hộ khẩu";
                dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
                dgvNhanKhau.Columns[0].Visible = false;
                //dgvNhanKhau.Columns[1].Visible = false;
                dgvNhanKhau.Columns[2].Visible = false;
                dgvNhanKhau.Columns[3].Visible = false;
                //dgvNhanKhau.Columns[4].Visible = false;
                //dgvNhanKhau.Columns[5].Visible = false;
                dgvNhanKhau.Columns[6].Visible = false;
                //dgvNhanKhau.Columns[7].Visible = false;
                //dgvNhanKhau.Columns[8].Visible = false;
                dgvNhanKhau.Columns[9].Visible = false;
                //dgvNhanKhau.Columns[10].Visible = false;
                dgvNhanKhau.Columns[11].Visible = false;
                dgvNhanKhau.Columns[12].Visible = false;
                dgvNhanKhau.Columns[13].Visible = false;
                dgvNhanKhau.Columns[14].Visible = false;
                dgvNhanKhau.Columns[15].Visible = false;
                dgvNhanKhau.Columns[16].Visible = false;
                dgvNhanKhau.Columns[17].Visible = false;
                dgvNhanKhau.Columns[18].Visible = false;
                dgvNhanKhau.Columns[19].Visible = false;
                dgvNhanKhau.Columns[20].Visible = false;
                dgvNhanKhau.Columns[21].Visible = false;
                dgvNhanKhau.Columns[22].Visible = false;
                dgvNhanKhau.Columns[23].Visible = false;
                //load phần sổ hộ khẩu
                txtDuong.Text = shk.Duong;
                txtAp.Text = shk.KhuPho.ToString();
                txtSoNha.Text = shk.SoNha;
                DataGridViewCellStyle style = new DataGridViewCellStyle();
                style.ForeColor = Color.AliceBlue;
                style.BackColor = Color.Red;
                for (int i = dgvNhanKhau.RowCount - 1; i >= 0; i--)
                {
                    if ((byte)dgvNhanKhau.Rows[i].Cells[21].Value != 0)
                    {
                        dgvNhanKhau.Rows[i].DefaultCellStyle = style;
                    }
                }
                if (shk.NgayXoa != null)
                {
                    groupBox1.Enabled = false;
                    groupBox2.Enabled = false;
                    btnThem.Enabled = false;
                    return;
                }
                frmThemNhanKhau tnk = new frmThemNhanKhau(_MaSHK);
                if (list_nkc.Count != 0)
                {
                    cmbHoTenMe.BindingContext = new BindingContext();
                    cmbHoTenCha.DataSource = list_nkc; //NhanKhauBLL.LoadNhanKhauBySoSHK(SoSHK);
                    cmbHoTenCha.DisplayMember = "HoTen";
                    cmbHoTenCha.ValueMember = "MaNK";
                    cmbHoTenCha.SelectedIndex = -1;            
                }
                else
                {
                    
                    DialogResult result= MessageBox.Show("Vui lòng thêm nhân khẩu cha", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    switch (result)
                    {
                        case DialogResult.OK:
                            
                            tnk.ShowDialog();
                            break;
                        //case DialogResult.Cancel:
                        //    break;
                    }
                }
                if (list_nkm.Count != 0)
                {
                    cmbHoTenCha.BindingContext = new BindingContext();
                    cmbHoTenMe.DataSource = list_nkm; //NhanKhauBLL.LoadNhanKhauBySoSHK(SoSHK);
                    cmbHoTenMe.DisplayMember = "HoTen";
                    cmbHoTenMe.ValueMember = "MaNK";
                    cmbHoTenMe.SelectedIndex = -1;
                    
                }
                else
                {
                   DialogResult result= MessageBox.Show("Vui lòng thêm nhân khẩu mẹ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                   switch (result)
                   { 
                       case DialogResult.OK:       
                           tnk.ShowDialog();
                           break;
                       //case DialogResult.Cancel:
                       //    break;
                   }
                }
                if (tnk.IsDisposed == false && tnk.Success == 1)
                {
                    btnTim_Click(sender, e);
                    dgvNhanKhau.DataSource = NhanKhauBLL.LoadNhanKhauByMaSHK(_MaSHK);
                    for (int i = dgvNhanKhau.RowCount - 1; i >= 0; i--)
                    {
                        if ((byte)dgvNhanKhau.Rows[i].Cells[21].Value != 0)
                        {
                            //MessageBox.Show("geg");
                            dgvNhanKhau.Rows[i].DefaultCellStyle = style;
                        }

                    }
                } 
            }
        }
        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtSoKhaiSinh.Text == "" || dateNgayCap.Text == "" || txtNguoiDangki.Text == "" || txtQueQuan.Text == ""|| txtHoTen.Text == "" || dateNgaySinh.Text == "" || txtNoiCap.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông báo nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (dateNgaySinh.Value > DateTime.Now)
            {
                MessageBox.Show("Ngày sinh không hợp lệ", "Thông báo nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                dateNgaySinh.Focus();
                return;
            }
            if (dateNgayCap.Value > DateTime.Now)
            {
                MessageBox.Show("Ngày cấp không hợp lệ", "Thông báo nhập liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                dateNgayCap.Focus();
                return;
            }
            //them khai sinh
            KhaiSinhPublic ks = new KhaiSinhPublic();
            ks.MaKS = DungChung.LoadMaNK();
            ks.SoKhaiSinh = int.Parse(txtSoKhaiSinh.Text);
            ks.NgayCap = DateTime.Parse(dateNgayCap.Text);
            ks.NoiCap =ThietLapBLL.LoadMaDiaChi();
            ks.NguoiDangki = txtNguoiDangki.Text;
            ks.MaQH = (byte)cmbQuanHeKS.SelectedValue;
            //ks.NguoiCap = txtCanBoHoTich.Text;
            //ks.ChucVu = txtCanBoHoTich.Text;
            ks.NguoiCap = (int)cmbNguoiCap.SelectedValue;
            ks.ChucVu = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).MaCV;
            ks.NoiSinh = (int)cmbNoiSinh.SelectedValue;
            //ks.HoTenCha = txtHoTenCha.Text;
            ks.MaNKC = (int)cmbHoTenCha.SelectedValue;
            ks.MaNKM = (int)cmbHoTenMe.SelectedValue;
            ks.NgayNhap = DateTime.Parse(dateNgayNhap.Text);
            //ks.CanBoDangKi = (int)cmbCanBoDangKi.SelectedValue;
            ks.CanBoDangKi = frmDangNhap.user_login.MaND;
            ks.NguoiNhan = (int)cmbNguoiNhan.SelectedValue;
            ks.CanBoHoTich = (int)cmbCanBoHoTich.SelectedValue;
            ks.ChucVuNguoiNhan = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue).MaCV;

            KhaiSinhBLL.InsertKhaiSinh(ks);

            //thêm nhân khẩu
            NhanKhauPublic nk = new NhanKhauPublic();
            nk.MaNK = DungChung.LoadMaNK();
            nk.HoTen = txtHoTen.Text;
            nk.NoiSinh = (int)cmbNoiSinh.SelectedValue;
            nk.MaKS = int.Parse(txtMaKS.Text);
            nk.NgaySinh = DateTime.Parse(dateNgaySinh.Text);
            if (rdtGioiTinhNam.Checked)
                nk.GioiTinh = true;
            else
                nk.GioiTinh = false;
            nk.QueQuan = txtQueQuan.Text;
            nk.MaDT = (byte)cmbQuocTich.SelectedValue;
            nk.MaQH = (byte)cmbQuanHeChuHo.SelectedValue;
            nk.NgheNghiep = "";
            nk.MaQT = (byte)cmbQuocTich.SelectedValue;
            //nk.MaKT = null;
            nk.MaTG = 1;
            nk.MaTD = 1;
            nk.NgayNhap = dateNgayNhap.Value;
            nk.LyDoNhap = 2;
            nk.CMND = "";
            nk.TenGoiKhac = "";
            nk.TinhTrang = 0;
            nk.MaSHK = SoHoKhauBLL.LoadSoHoKhauBySoSHK(int.Parse(txtSoSHK.Text)).MaSHK;

            NhanKhauBLL.InsertNhanKhauKS(nk);
            
            MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //load lai form
            //UCKhaiSinh_Load(sender, e);
            btnTim_Click(sender, e);
            groupBox1.Enabled = true;
            groupBox2.Enabled = true;

            //kiem tra khi insert
            txtHoTen.ResetText();
            cmbNoiSinh.Refresh() ;
            txtQueQuan.ResetText();
            txtNguoiDangki.ResetText();
            txtSoKhaiSinh.ResetText();
            groupBox1.ResumeLayout();
            dateNgaySinh.ResetValue();
            dateNgayCap.ResetValue();
            cmbCanBoHoTich.Refresh();
            cmbNguoiCap.Refresh();
            txtDanTocCha.ResetText();
            txtDanTocMe.ResetText();
            txtQuocTichCha.ResetText();
            txtQuocTichMe.ResetText();
        }

        
        private void txtSoKhaiSinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoKhaiSinh.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void cmbHoTenCha_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //MessageBox.Show("cho' Phuong");
            NhanKhauPublic father = NhanKhauBLL.LoadNhanKhauByMaNK((int)cmbHoTenCha.SelectedValue);

            txtDanTocCha.Text = DanTocBLL.LoadDanTocByMaDT(father.MaDT).TenDanToc;
            txtDanTocCha.Enabled = false;
            txtQuocTichCha.Text = QuocTichBLL.LoadQuocTichByMaQT(father.MaQT).TenQuocTich;
            txtQuocTichCha.Enabled = false;
        }

        private void cmbHoTenMe_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NhanKhauPublic mother = NhanKhauBLL.LoadNhanKhauByMaNK((int)cmbHoTenMe.SelectedValue);

            txtDanTocMe.Text = DanTocBLL.LoadDanTocByMaDT(mother.MaDT).TenDanToc;
            txtDanTocMe.Enabled = false;
            txtQuocTichMe.Text = QuocTichBLL.LoadQuocTichByMaQT(mother.MaQT).TenQuocTich;
            txtQuocTichMe.Enabled = false;
        }

        private void cmbNguoiNhan_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVuNguoiNhan.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuNguoiNhan.Enabled = false;
        }

        private void txtSoSHK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoSHK.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
            if (e.KeyChar == 13)
                btnTim_Click(sender, e);
        }

        //private void txtQuanHeChuHo_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if ( txtQuanHeChuHo.Text.Length > 11 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}

        private void txtHoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtHoTen.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        //private void txtNoiSinh_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if (txtNoiSinh.Text.Length > 49 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}

        private void txtQueQuan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtQueQuan.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        //private void txtChucVu_KeyPress(object sender, KeyPressEventArgs e)
        //{
        //    if (txtChucVu.Text.Length > 15 && !Char.IsControl(e.KeyChar))
        //    {
        //        e.Handled = true;
        //    }
        //}

        private void txtNoiCap_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtNoiCap.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void cmbNguoiCap_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVu.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVu.Enabled = false;
        }

        private void txtNguoiDangKi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtNguoiDangki.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoKhaiSinh_Leave(object sender, EventArgs e)
        {
            string sokhaisinh = txtSoKhaiSinh.Text;
            if (txtSoKhaiSinh.Text == "")
            {
                txtSoKhaiSinh.Focus();
            }
            if (KhaiSinhBLL.TestSoKhaiSinh(int.Parse(txtSoKhaiSinh.Text)) ==false)
            {
                this.balloonTip2.ShowBalloon(txtSoKhaiSinh);
                this.balloonTip2.SetBalloonCaption(txtSoKhaiSinh, "Thông báo");
                this.balloonTip2.SetBalloonText(txtSoKhaiSinh, "Đã có Số khai sinh này trong hệ thống");
                txtSoKhaiSinh.Focus();
            }
        }
        
    }
}
